from datetime import datetime, timedelta
import calendar
import random


class DataTool(object):

    def current_date(self):
        year = datetime.now().strftime('%Y')
        # 去除左边的开头的0: '01' - '1'
        month = datetime.now().strftime('%m').lstrip('0')
        return year, month

    def get_start_time_of_month(self):
        current_date = datetime.now()
        start_time_of_month = current_date.strftime('%Y-%m-01 00:00:00')
        return start_time_of_month

    def get_end_time_of_month(self):
        current_date = datetime.now()
        year, month = current_date.year, current_date.month
        # 忽略第一个返回值
        _, last_day_num = calendar.monthrange(year, month)
        last_time_of_month = datetime(year, month, last_day_num).strftime('%Y-%m-%d 23:59:59')
        return last_time_of_month

    def get_random_date(self, start_date, end_date):
        start = datetime.strptime(start_date, "%Y-%m-%d")
        end = datetime.strptime(end_date, "%Y-%m-%d")
        delta = end - start
        total_seconds = delta.days * 24 * 3600 + delta.seconds
        random_seconds = random.randint(0, total_seconds)
        return start + timedelta(seconds=random_seconds)


if __name__ == '__main__':
    test = DataTool()
    result0 = test.get_random_date('2025-01-23', '2025-12-23')
    print(result0)
    print(type(result0))
